//
//  XVVReleasePersonInfomation.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/31.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVReleasePersonInfomation.h"
#import "XXTool.h"
#import <AddressBook/AddressBook.h>

@interface XVVReleasePersonInfomation ()<UIActionSheetDelegate,UIAlertViewDelegate>

@property (nonatomic,strong) UIView* content;

@property (nonatomic,strong) UIImageView *imgHead;

@property (nonatomic,strong) UILabel *userName;

@property (nonatomic,strong) UILabel *companyName;

@property (nonatomic,strong) UIView *line;

@property (nonatomic,strong) UILabel *showNoInfoTip;

@property (nonatomic,strong) DKButton *saveInformationButton;

@property (nonatomic,strong) UIImageView *exhibitorTipImg;

@property (nonatomic,strong) UILabel *exhibitorTipLabel;

@property (nonatomic,strong) UILabel *baseInformation;

@end

@implementation XVVReleasePersonInfomation
{
    CGFloat baseGap;
    CGFloat labelWidth;
    XVVReleasePersonInfomationType nowVCType;
    XMLRealsePersonInformation *data;
}
#pragma mark getter
-(UIImageView*)imgHead
{
    if (_imgHead==nil)
    {
        _imgHead = [[UIImageView alloc] init];
        _imgHead.contentMode = UIViewContentModeScaleToFill;
        _imgHead.clipsToBounds = YES;
        [_content addSubview:_imgHead];
    }
    return _imgHead;
}

-(UILabel*)userName
{
    if (_userName==nil)
    {
        _userName = [[UILabel alloc] init];
        _userName.font = [XXTool systemFontOfSize:15];
        _userName.textColor = [UIColor blackColor];
        _userName.textAlignment = NSTextAlignmentCenter;
        _userName.numberOfLines = 1;
        [_content addSubview:_userName];
    }
    return _userName;
}

-(UILabel*)companyName
{
    if (_companyName==nil)
    {
        _companyName = [[UILabel alloc] init];
        _companyName.font = [XXTool systemFontOfSize:16.5f];
        _companyName.textColor = [UIColor blackColor];
        _companyName.numberOfLines = 0;
        [_content addSubview:_companyName];
    }
    return _companyName;
}

-(UIView*)line
{
    if (_line==nil)
    {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [XXTool colorWithProject];
        [_content addSubview:_line];
    }
    return _line;
}

-(UILabel*)showNoInfoTip
{
    if (_showNoInfoTip==nil)
    {
        _showNoInfoTip = [[UILabel alloc] init];
        _showNoInfoTip.font = [XXTool systemFontOfSize:16];
        _showNoInfoTip.textColor = [UIColor grayColor];
        _showNoInfoTip.text = NSLocalizedString(@"暂无更多相关信息", nil);
        _showNoInfoTip.textAlignment = NSTextAlignmentCenter;
        [_content addSubview:_showNoInfoTip];
    }
    return _showNoInfoTip;
}

-(DKButton*)saveInformationButton
{
    if (_saveInformationButton==nil)
    {
        _saveInformationButton = [[DKButton alloc] init];
        _saveInformationButton.image = [UIImage imageNamed:@"名片刷新图标"];
        _saveInformationButton.imageSize = CGSizeMake(18, 18);
        [_saveInformationButton setColor:[UIColor clearColor] ForState:DKButtonStateNormal];
        [_saveInformationButton addTarget:self Select:@selector(showSaveSheet) ForEvent:DKButtonEventPressedEnd];
        _saveInformationButton.imageRenderingMode = DKButtonRenderingModeAlwaysTemplate;
        _saveInformationButton.imageTintColor = [XXTool colorWithProject];
        [_content addSubview:_saveInformationButton];
    }
    return _saveInformationButton;
}

-(UIImageView*)exhibitorTipImg
{
    if (_exhibitorTipImg==nil)
    {
        _exhibitorTipImg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"观众的标签"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        _exhibitorTipImg.tintColor = [XXTool colorWithProject];
        _exhibitorTipImg.contentMode = UIViewContentModeScaleAspectFill;
        [_content addSubview:_exhibitorTipImg];
    }
    return _exhibitorTipImg;
}

-(UILabel*)exhibitorTipLabel
{
    if (_exhibitorTipLabel==nil)
    {
        _exhibitorTipLabel = [[UILabel alloc] init];
        _exhibitorTipLabel.textColor = [UIColor whiteColor];
        if ([XXTool didCurrentLanguageChinese])
        {
            _exhibitorTipLabel.font = [XXTool systemFontOfSize:17];
        }
        else
        {
            _exhibitorTipLabel.font = [XXTool systemFontOfSize:14];
        }
        _exhibitorTipLabel.textAlignment = NSTextAlignmentCenter;
        [_content addSubview:_exhibitorTipLabel];
    }
    return _exhibitorTipLabel;
}

-(UILabel*)baseInformation
{
    if (_baseInformation==nil)
    {
        _baseInformation = [[UILabel alloc] init];
        _baseInformation.font = [XXTool systemFontOfSize:15];
        _baseInformation.textColor = [UIColor grayColor];
        _baseInformation.numberOfLines = 0;
        [_content addSubview:_baseInformation];
    }
    return _baseInformation;
}

-(UIView*)content
{
    if (_content==nil)
    {
        _content = [[UIView alloc] init];
        _content.backgroundColor = [UIColor whiteColor];
        _content.layer.shadowOpacity = 1;
        _content.layer.shadowColor = [UIColor colorWithRed:214/255.0 green:215/255.0 blue:216/255.0 alpha:1.0].CGColor;
        _content.layer.shadowOffset = CGSizeMake(0,1);
        _content.layer.shadowRadius = 2;
        _content.layer.masksToBounds = NO;
        [self addSubview:_content];
    }
    return _content;
}

-(void)initUI
{
    [self content];
    [self imgHead];
    [self userName];
    [self exhibitorTipImg];
    [self exhibitorTipLabel];
    [self companyName];
    [self line];
    [self saveInformationButton];
    [self baseInformation];
}

-(void)initParameter
{
    baseGap = 10;
}

-(void)refreshLayout:(CGRect)frame
{
    labelWidth = frame.size.width-60-baseGap*2-54;
    
    CGFloat companyHeight = [XXTool getTextHeightByText:_companyName.text
                                                   Font:_companyName.font
                                                  Width:labelWidth];
    
    CGFloat informationHeight = [XXTool getTextHeightByText:_baseInformation.text
                                                       Font:_baseInformation.font
                                                      Width:labelWidth];
    
    
    _imgHead.frame = CGRectMake(baseGap
                                , baseGap
                                , 60
                                , 60);
    _imgHead.layer.cornerRadius = _imgHead.frame.size.width/2;
    
    _userName.frame = CGRectMake(_imgHead.frame.origin.x
                                 , _imgHead.frame.size.height+_imgHead.frame.origin.y
                                 , _imgHead.frame.size.width
                                 , 30);
    _exhibitorTipImg.frame = CGRectMake(0
                                        , _userName.frame.size.height+_userName.frame.origin.y
                                        , 65
                                        , 36);
    _exhibitorTipLabel.frame = CGRectMake(0, _exhibitorTipImg.frame.origin.y
                                          , _exhibitorTipImg.frame.size.width
                                          , _exhibitorTipImg.frame.size.height);
    _companyName.frame = CGRectMake(_imgHead.frame.size.width+_imgHead.frame.origin.x+baseGap
                                    , _imgHead.frame.origin.y+7
                                    , labelWidth
                                    , companyHeight);
    
    _saveInformationButton.frame = CGRectMake(_companyName.frame.size.width+_companyName.frame.origin.x
                                              , 15
                                              , 30
                                              , 30);
    
    _line.frame = CGRectMake(_companyName.frame.origin.x
                             , _companyName.frame.size.height+_companyName.frame.origin.y+6
                             , frame.size.width-baseGap*2-_imgHead.frame.size.width-8*2, 0.5);
    
    _content.frame = CGRectMake(8, 10, frame.size.width-8*2
                                , (companyHeight+_line.frame.size.height+17+6*2+informationHeight)<=139?152:(companyHeight+0.5+17+6*2+informationHeight+13));
    _content.layer.shadowPath = [UIBezierPath bezierPathWithRect:_content.layer.bounds].CGPath;
    
    _baseInformation.frame = CGRectMake(_companyName.frame.origin.x
                                        , _line.frame.size.height+_line.frame.origin.y+6
                                        , _content.xx_width-baseGap-_companyName.xx_origin.x
                                        , informationHeight);
    
    _showNoInfoTip.frame = CGRectMake(_companyName.frame.origin.x
                                      , 0
                                      , _content.frame.size.width-_imgHead.frame.size.width-_imgHead.frame.origin.x-7
                                      , _content.frame.size.height);
}

-(void)showSaveSheet
{
    if (nowVCType==XVVReleasePersonInfomationVisitor)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:NSLocalizedString(@"保存到通讯录", nil)
                                                  otherButtonTitles:NSLocalizedString(@"取消收藏", nil), nil];
        sheet.delegate = self;
        [sheet showInView:self];
    }
    else if (nowVCType==XVVReleasePersonInfomationSeenU)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:NSLocalizedString(@"保存到通讯录", nil)
                                                  otherButtonTitles:NSLocalizedString(@"删除", nil), nil];
        sheet.delegate = self;
        [sheet showInView:self];
    }
    else
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:NSLocalizedString(@"保存到通讯录", nil)
                                                  otherButtonTitles:nil, nil];
        sheet.delegate = self;
        [sheet showInView:self];
    }
}

#pragma mark ActionSheetDelegate  这段应该用delegate写到vc里面，偷懒了，同样的地方还有，以后注意
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) //保存到通讯录
    {
        [self addPeople];
    }
    else if (buttonIndex==1&&nowVCType==XVVReleasePersonInfomationVisitor)  //展商收藏夹中取消收藏观众
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                                                        message:NSLocalizedString(@"是否确认删除该用户名片？", nil)
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                              otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alter show];
    }
    else if (buttonIndex==1&&nowVCType==XVVReleasePersonInfomationSeenU) //展商看过你的人中删除观众
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                                                        message:NSLocalizedString(@"是否确认删除该用户名片？", nil)
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                              otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alter show];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        [DKHTTPRequest sendDataWithShortUrl:@"msg_dellooked"
                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=%@&passwd=%@&visitorid=%@",[UserInfo shareUserInfo].userID,[UserInfo shareUserInfo].userPassword,data.visitor_id]]
                                 ImageArray:nil
                                    TimeOut:10
                                 Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                     if ([[serverDic objectForKey:@"code"] intValue]==200)
                                     {
                                         
                                         if ([_delegate respondsToSelector:@selector(personInformationDeleteSuccess)])
                                         {
                                             [_delegate personInformationDeleteSuccess];
                                         }
                                     }
                                     return YES;
                                 } IsBuffering:NO
                          IsNeedReplaceWrap:NO
                            IsUseUpLoadHead:NO];
     }
}

//保存联系人到通讯录
-(void)addPeople
{
    /*
    //添加到通讯录,判断通讯录是否存在
    if ([self isExistContactPerson])
      {//存在，返回
                 //提示
    if (IS_iOS8)
    {
        UIAlertController *tipVc  = [UIAlertController alertControllerWithTitle:@"提示" message:@"联系人已存在..." preferredStyle:UIAlertControllerStyleAlert];
         UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                 [[XXTool getViewController:self] dismissViewControllerAnimated:YES completion:nil];
             }];
        [tipVc addAction:cancleAction];
        [[XXTool getViewController:self] presentViewController:tipVc animated:YES completion:nil];
    }
    else
    {
               UIAlertView *tip = [[UIAlertView alloc] initWithTitle:@"提示" message:@"联系人已存在..." delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
               [tip show];
                 //        [tip release];
    }
        return;
    }
    else
    {//不存在  添加
     */
    [self creatNewRecord];
    //}
}

/*
- (BOOL)isExistContactPerson{
         //这个变量用于记录授权是否成功，即用户是否允许我们访问通讯录
         int __block tip=0;
    
         BOOL __block isExist = NO;
         //声明一个通讯簿的引用
         ABAddressBookRef addBook =nil;
        //因为在IOS6.0之后和之前的权限申请方式有所差别，这里做个判断
         if (IS_iOS7) {
                 //创建通讯簿的引用，第一个参数暂时写NULL，官方文档就是这么说的，后续会有用，第二个参数是error参数
                 CFErrorRef error = NULL;
                 addBook=ABAddressBookCreateWithOptions(NULL, &error);
                 //创建一个初始信号量为0的信号
                 dispatch_semaphore_t sema=dispatch_semaphore_create(0);
                 //申请访问权限
                 ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error)        {
                         //greanted为YES是表示用户允许，否则为不允许
                         if (!greanted) {
                                 tip=1;
                
                             }else{
                                     //获取所有联系人的数组
                                     CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
                                     //获取联系人总数
                                     CFIndex number = ABAddressBookGetPersonCount(addBook);
                                     //进行遍历
                                     for (NSInteger i=0; i<number; i++) {
                                             //获取联系人对象的引用
                                             ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
                                             //获取当前联系人名字
                                             NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
                        
                                             if ([firstName isEqualToString:[_infoDictionary objectForKey:@"name"]]) {
                                                     isExist = YES;
                                                 }
                        
                         //                    //获取当前联系人姓氏
                         //                    NSString*lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
                        
                         //获取当前联系人中间名
                         //                    NSString*middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
                         //                    //获取当前联系人的名字前缀
                         //                    NSString*prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));
                         //                    //获取当前联系人的名字后缀
                         //                    NSString*suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));
                         //                    //获取当前联系人的昵称
                         //                    NSString*nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
                         //                    //获取当前联系人的名字拼音
                         //                    NSString*firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));
                         //                    //获取当前联系人的姓氏拼音
                         //                    NSString*lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));
                         //                    //获取当前联系人的中间名拼音
                         //                    NSString*middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));
                         //                    //获取当前联系人的公司
                         //                    NSString*organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));
                         //                    //获取当前联系人的职位
                         //                    NSString*job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));
                         //                    //获取当前联系人的部门
                         //                    NSString*department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));
                         //                    //获取当前联系人的生日
                         //                    NSString*birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty));
                         //                    NSMutableArray * emailArr = [[NSMutableArray alloc]init];
                        //                    //获取当前联系人的邮箱 注意是数组
                         //                    ABMultiValueRef emails= ABRecordCopyValue(people, kABPersonEmailProperty);
                         //                    for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
                         //                        [emailArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j))];
                         //                    }
                         //                    //获取当前联系人的备注
                         //                    NSString*notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));
                         //                    //获取当前联系人的电话 数组
                         //                    NSMutableArray * phoneArr = [[NSMutableArray alloc]init];
                         //                    ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);
                         //                    for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
                         //                        [phoneArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
                         //                    }
                         //                    //获取创建当前联系人的时间 注意是NSDate
                         //                    NSDate*creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));
                         //                    //获取最近修改当前联系人的时间
                         //                    NSDate*alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty));
                         //                    //获取地址
                         //                    ABMultiValueRef address = ABRecordCopyValue(people, kABPersonAddressProperty);
                         //                    for (int j=0; j<ABMultiValueGetCount(address); j++) {
                         //                        //地址类型
                         //                        NSString * type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j));
                         //                        NSDictionary * temDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
                         //                        //地址字符串，可以按需求格式化
                         //                        NSString * adress = [NSString stringWithFormat:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",[temDic valueForKey:(NSString*)kABPersonAddressCountryKey],[temDic valueForKey:(NSString*)kABPersonAddressStateKey],[temDic valueForKey:(NSString*)kABPersonAddressCityKey],[temDic valueForKey:(NSString*)kABPersonAddressStreetKey],[temDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
                         //                    }
                         //                    //获取当前联系人头像图片
                         //                    NSData*userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
                         //                    //获取当前联系人纪念日
                         //                    NSMutableArray * dateArr = [[NSMutableArray alloc]init];
                         //                    ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty);
                         //                    for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
                         //                        //获取纪念日日期
                         //                        NSDate * data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
                         //                        //获取纪念日名称
                         //                        NSString * str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
                         //                        NSDictionary * temDic = [NSDictionary dictionaryWithObject:data forKey:str];
                         //                        [dateArr addObject:temDic];
                         //                    }
                                         }
                    
                    
                                 }
                         //发送一次信号
                         dispatch_semaphore_signal(sema);
                     });
                 //等待信号触发
                 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
            }else{
            
                 //IOS6之前
                addBook =ABAddressBookCreate();
            
                  //获取所有联系人的数组
                   CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
                   //获取联系人总数
                CFIndex number = ABAddressBookGetPersonCount(addBook);
                   //进行遍历
                    for (NSInteger i=0; i<number; i++) {
                      //获取联系人对象的引用
                          ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
                          //获取当前联系人名字
                          NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
                
                          if ([firstName isEqualToString:[_infoDictionary objectForKey:@"name"]]) {
                                 isExist = YES;
                              }
                      }
              }
    
       if (tip) {
        //设置提示
              if (IS_iOS8) {
                      UIAlertController *tipVc  = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"请您设置允许APP访问您的通讯录\nSettings>General>Privacy" preferredStyle:UIAlertControllerStyleAlert];
                     UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                               [self dismissViewControllerAnimated:YES completion:nil];
                           }];
                       [tipVc addAction:cancleAction];
                         [tipVc presentViewController:tipVc animated:YES completion:nil];
                     }else{
                          UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"请您设置允许APP访问您的通讯录\nSettings>General>Privacy" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                           [alart show];
                          //非ARC
               //            [alart release];
                        }
            }
        return isExist;
    
}
*/

//创建新的联系人
- (void)creatNewRecord
{
    //获取用户名
    NSString *userName = @"";
    if (data.vname==nil&&data.lookname==nil)
    {
        userName = data.username;
    }
    else if (data.lookname==nil)
    {
        userName = data.vname;
    }
    else
    {
        userName = data.lookname;
    }
    
    //获取手机号码
    NSString *userPhone = @"";
    if (data.userphone==nil)
    {
        userPhone = data.mobile;
    }
    else if (data.mobile==nil)
    {
        userPhone = data.userphone;
    }
    
    //获取座机号码
    NSString *userTel = @"";
    if (data.tel==nil)
    {
        userTel = data.telphone;
    }
    else if (data.telphone==nil)
    {
        userTel = data.tel;
    }
    
     CFErrorRef error = NULL;

     //创建一个通讯录操作对象
     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

     //创建一条新的联系人纪录
     ABRecordRef newRecord = ABPersonCreate();

     //为新联系人记录添加属性值
     ABRecordSetValue(newRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)userName, &error);

    //创建一个多值属性(电话)
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)userPhone, kABPersonPhoneMobileLabel, NULL);
    if (![userTel isEqualToString:@""])
    {
        ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)userTel, kABWorkLabel, NULL);
    }
    ABRecordSetValue(newRecord, kABPersonPhoneProperty, multi, &error);


     //添加记录到通讯录操作对象
     ABAddressBookAddRecord(addressBook, newRecord, &error);

     //保存通讯录操作对象
     ABAddressBookSave(addressBook, &error);

     //通过此接口访问系统通讯录
     ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    
             if (granted) {
                     //显示提示
                     if (IS_iOS8) {
                             UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"已成功添加到通讯录", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
                             UIAlertAction *alertAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                                     //[[XXTool getViewController:self] dismissViewControllerAnimated:YES completion:nil];

                                 }];
                             [alertVc addAction:alertAction];
                             [[XXTool getViewController:self] presentViewController:alertVc animated:YES completion:nil];
                         }else{
                
                                 UIAlertView *tipView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"已成功添加到通讯录", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil, nil];
                                 [tipView show];
                             }
                 }
         });
    
    
    
     CFRelease(multi);
     CFRelease(newRecord);
     CFRelease(addressBook);
    
}

-(void)setData:(XMLRealsePersonInformation *)model personType:(XVVReleasePersonInfomationType)useType
{
    
    if (model.vname==nil&&model.lookname==nil)
    {
        _userName.text = model.username;
    }
    else if (model.lookname==nil)
    {
        _userName.text = model.vname;
    }
    else
    {
        _userName.text = model.lookname;
    }
    
    if ([_userName.text isEqualToString:@""])
    {
        _line.hidden = YES;
        _saveInformationButton.hidden = YES;
        _baseInformation.hidden = YES;
        self.showNoInfoTip.hidden = NO;
    }
    else
    {
        _line.hidden = NO;
        _saveInformationButton.hidden = NO;
        _baseInformation.hidden = NO;
        self.showNoInfoTip.hidden = YES;
    }
    
    nowVCType = useType;
    data = model;
    if (useType==XVVReleasePersonInfomationVisitor||useType==XVVReleasePersonInfomationSeenU)
    {
        _exhibitorTipLabel.text = NSLocalizedString(@"观众", nil);
        _companyName.text = model.compname;
        [_imgHead setImageWithUrl:model.head_url placerHolder:[UIImage imageNamed:@"消息默认头像"]];

    }
    else
    {
        _exhibitorTipLabel.text = NSLocalizedString(@"参展商", nil);
        _companyName.text = model.cpname;
        [_imgHead setImageWithUrl:model.headphoto placerHolder:[UIImage imageNamed:@"消息默认头像"]];
    }
    
    if (useType==XVVReleasePersonInfomationDetail)
    {
        _exhibitorTipLabel.text = NSLocalizedString(@"观众", nil);
    }
    
    NSString* information = @"";
    
    if (useType==XVVReleasePersonInfomationDetail)
    {
        if (![model.userphone isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"手机", nil),model.userphone];
        }
        if (![model.usergh isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"固话", nil),model.usergh];
        }
        if (![model.userfax isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"传真", nil),model.userfax];
        }
        if (![model.usermail isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"邮箱", nil),model.usermail];
        }
    }
    else
    {
        if (![model.mobile isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"手机", nil),model.mobile];
        }
        if (model.tel!=nil&&![model.tel isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"固话", nil),model.tel];
        }
        if (model.telphone!=nil&&![model.telphone isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"固话", nil),model.telphone];
        }
        if (![model.fax isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"传真", nil),model.fax];
        }
        if (![model.mail isEqualToString:@""])
        {
            information = [NSString stringWithFormat:@"%@\n%@:%@",information,NSLocalizedString(@"邮箱", nil),model.mail];
        }
    }
    _baseInformation.text = information;
    [self refreshLayout:self.frame];
}

+(CGFloat)getHeight:(XMLRealsePersonInformation *)model personType:(XVVReleasePersonInfomationType)useType
{
    CGFloat labelWidth = [UIScreen mainScreen].bounds.size.width-60-10*2-54;
    
    CGFloat companyHeight = 0;
    if (useType==XVVReleasePersonInfomationVisitor||useType==XVVReleasePersonInfomationSeenU)
    {
        companyHeight = [XXTool getTextHeightByText:model.compname
                               Font:[XXTool systemFontOfSize:16.5f]
                                              Width:labelWidth];
    }
    else
    {
        companyHeight = [XXTool getTextHeightByText:model.cpname
                                               Font:[XXTool systemFontOfSize:16.5f]
                                              Width:labelWidth];
    }
    
    NSString* information = @"";
    if (![model.mobile isEqualToString:@""])
    {
        information = [NSString stringWithFormat:@"手机:%@",model.mobile];
    }
    if (![model.tel isEqualToString:@""])
    {
        information = [NSString stringWithFormat:@"%@\n座机:%@",information,model.tel];
    }
    if (![model.fax isEqualToString:@""])
    {
        information = [NSString stringWithFormat:@"%@\n传真:%@",information,model.fax];
    }
    if (![model.mail isEqualToString:@""])
    {
        information = [NSString stringWithFormat:@"%@\n邮箱:%@",information,model.mail];
    }

    
    CGFloat informationHeight = [XXTool getTextHeightByText:information
                                                       Font:[XXTool systemFontOfSize:15]
                                                      Width:labelWidth];
    if ((companyHeight+0.5+17+6*2+informationHeight)<=139)
    {
        return 139+13+20;
    }
    else
    {
        return companyHeight+0.5+17+6*2+informationHeight+13+20;
    }
}

@end
